unit uRelRazao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UrelatorioBase, RLPDFFilter, RLXLSFilter, RLFilters,
  RLHTMLFilter, RLDraftFilter, DB, DBTables, RLReport, StdCtrls, ExtCtrls,
  Buttons, Mask, ToolEdit;

type
  TfRelRazao = class(TFrelatorioBase)
    RLBand2: TRLBand;
    RLLabel1: TRLLabel;
    RLLabel2: TRLLabel;
    RLLabel3: TRLLabel;
    RLLabel4: TRLLabel;
    RLLabel5: TRLLabel;
    RLLabel6: TRLLabel;
    RLLabel7: TRLLabel;
    RLLabel8: TRLLabel;
    grp1: TGroupBox;
    lbl1: TLabel;
    lbl2: TLabel;
    DTINI: TDateEdit;
    DTFIM: TDateEdit;
    grp2: TGroupBox;
    lbl3: TLabel;
    lbl4: TLabel;
    medt1: TMaskEdit;
    medt2: TMaskEdit;
    RLGroup1: TRLGroup;
    QPesquisaCCON_CODIGO: TIntegerField;
    QPesquisaCCON_COGIGOCONTABIL: TStringField;
    QPesquisaCCON_DESCRICAO: TStringField;
    QPesquisaTRANS_DEB_CRE: TStringField;
    QPesquisaDEB_VAL: TFloatField;
    QPesquisaCRE_VAL: TFloatField;
    QPesquisaTRANS_HISTORICO1: TStringField;
    QPesquisaTRANS_HISTORICO2: TStringField;
    QPesquisaTRANS_DATA: TDateTimeField;
    QPesquisaCCON_NATUREZA_CONTA: TStringField;
    RLBand4: TRLBand;
    RLDBText10: TRLDBText;
    RLDBText11: TRLDBText;
    RLBand3: TRLBand;
    RLDBText3: TRLDBText;
    RLDBText4: TRLDBText;
    RLDBText5: TRLDBText;
    RLDBText6: TRLDBText;
    rlblRlSaldo: TRLLabel;
    rlblDebCred: TRLDBText;
    RLDBText2: TRLDBText;
    RLBand5: TRLBand;
    RLDBResult1: TRLDBResult;
    RLDBResult2: TRLDBResult;
    rlbl1: TRLLabel;
    rlbl2: TRLLabel;
    rlblSaldoInicial: TRLLabel;
    rlblDebCredInicial: TRLLabel;
    procedure btnPesquisarClick(Sender: TObject);
    procedure RLBand3BeforePrint(Sender: TObject; var PrintIt: Boolean);
    procedure RLBand4BeforePrint(Sender: TObject; var PrintIt: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    SaldoInicial : Double;
    Saldo : Double;
    function getSaldoInicial(codEmpresa, codConta : Integer; ano, mes: String):Double;
  public
    { Public declarations }
  end;

var
  fRelRazao: TfRelRazao;

implementation

uses UGeral, uDMContabilidade;

{$R *.dfm}

procedure TfRelRazao.btnPesquisarClick(Sender: TObject);
var
  sql : TStringList;
begin
  try
    sql := TStringList.Create;
    sql.Add('    SELECT VW_LANCAMENTO_CONTABIL.CCON_CODIGO');
    sql.Add('          ,CONTA_CONTABIL.CCON_COGIGOCONTABIL');
    sql.Add('          ,CONTA_CONTABIL.CCON_DESCRICAO');
    sql.Add('          ,VW_LANCAMENTO_CONTABIL.TRANS_DEB_CRE');
    sql.Add('          ,CASE');
    sql.Add('            WHEN VW_LANCAMENTO_CONTABIL.TRANS_DEB_CRE = ''D'' THEN');
    sql.Add('              VW_LANCAMENTO_CONTABIL.TRANS_VALOR');
    sql.Add('            WHEN VW_LANCAMENTO_CONTABIL.TRANS_DEB_CRE = ''C'' THEN');
    sql.Add('              NULL');
    sql.Add('           END DEB_VAL');
    sql.Add('          ,CASE');
    sql.Add('            WHEN VW_LANCAMENTO_CONTABIL.TRANS_DEB_CRE = ''C'' THEN');
    sql.Add('              VW_LANCAMENTO_CONTABIL.TRANS_VALOR');
    sql.Add('            WHEN VW_LANCAMENTO_CONTABIL.TRANS_DEB_CRE = ''D'' THEN');
    sql.Add('              NULL');
    sql.Add('          END CRE_VAL');
    sql.Add('          ,VW_LANCAMENTO_CONTABIL.TRANS_VALOR');
    sql.Add('          ,VW_LANCAMENTO_CONTABIL.TRANS_HISTORICO1');
    sql.Add('          ,VW_LANCAMENTO_CONTABIL.TRANS_HISTORICO2');
    sql.Add('          ,VW_LANCAMENTO_CONTABIL.TRANS_DATA');
    sql.Add('          ,CONTA_CONTABIL.CCON_NATUREZA_CONTA');
    sql.Add('     FROM  VW_LANCAMENTO_CONTABIL');
    sql.Add('          ,CONTA_CONTABIL');
    sql.Add('     WHERE VW_LANCAMENTO_CONTABIL.CCON_CODIGO = CONTA_CONTABIL.CCON_CODIGO');
    sql.Add('  ORDER BY CONTA_CONTABIL.CCON_COGIGOCONTABIL');
    sql.Add('          ,VW_LANCAMENTO_CONTABIL.TRANS_DATA');
    QPesquisa.SQL.Clear;
    QPesquisa.SQL.Add(sql.Text);
    // Definindo a mascara
    TCurrencyField(QPesquisaCCON_COGIGOCONTABIL).EditMask := DMContabilidade.getMascaraCodContabil(codigoEmpresa);
    QPesquisa.Open;
  finally
    FreeAndNil(sql);
  end;
  inherited;

end;

function TfRelRazao.getSaldoInicial(codEmpresa, codConta: Integer; ano,
  mes: String): Double;
begin
  {TODO: Implementar Saldo inicial}
  Result := 0;
end;

procedure TfRelRazao.RLBand3BeforePrint(Sender: TObject;
  var PrintIt: Boolean);
var
  dSaldo : Double;
begin
  dSaldo := 0;
  if QPesquisaCCON_NATUREZA_CONTA.AsString = QPesquisaTRANS_DEB_CRE.AsString then
  begin
    dSaldo := SaldoInicial + (QPesquisaDEB_VAL.AsFloat + QPesquisaCRE_VAL.AsFloat)
  end else begin
    dSaldo := SaldoInicial + (QPesquisaDEB_VAL.AsFloat - QPesquisaCRE_VAL.AsFloat)
  end;

  if dSaldo < 0 then
  begin
    dSaldo := dSaldo*(-1);
    if QPesquisaCCON_NATUREZA_CONTA.AsString = 'C' then
      rlblDebCred.Caption := 'D'
    else
      rlblDebCred.Caption := 'C';
    rlblRlSaldo.Caption := FormatFloat('0,.00', dSaldo);
  end;

  if Trim(QPesquisaTRANS_HISTORICO2.AsString) = '' then
    RLBand3.Height := 16
  else
    RLBand3.Height := 31;
end;

procedure TfRelRazao.RLBand4BeforePrint(Sender: TObject;
  var PrintIt: Boolean);
var
  dSaldo : Double;
begin
  dSaldo := 0;
  inherited;
  dSaldo := getSaldoInicial(Codigoempresa, QPesquisaCCON_CODIGO.AsInteger, '01', '2013');
  SaldoInicial := dSaldo;
  if dSaldo < 0 then
  begin
    dSaldo := dSaldo*(-1);
    if QPesquisaCCON_NATUREZA_CONTA.AsString = 'C' then
      rlblDebCredInicial.Caption := 'D'
    else
      rlblDebCredInicial.Caption := 'C';
  end else
    rlblDebCredInicial.Caption := QPesquisaCCON_NATUREZA_CONTA.AsString;
  rlblSaldoInicial.Caption := FormatFloat('0,.00', dSaldo);
end;

procedure TfRelRazao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  fRelRazao := nil;
  inherited;

end;

end.
